package com.chice.scangundemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.Toast;

import com.chice.scangundemo.scangunpakage.ScanGun;

import java.util.ArrayList;
import java.util.List;

public class PagerActivity extends FragmentActivity {

    private ScanGun mScanGun;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pager);

        // 设置key事件最大间隔，默认20ms，部分低端扫码枪效率低
        ScanGun.setMaxKeysInterval(50);
        mScanGun = new ScanGun(new ScanGun.ScanGunCallBack() {
            @Override
            public void onScanFinish(String scanResult) {
                Toast.makeText(PagerActivity.this, "数据： "+scanResult, Toast.LENGTH_SHORT).show();

            }
        });

        init();
    }

    public void init() {
        //构造适配器
        List<Fragment> fragments=new ArrayList<Fragment>();
        fragments.add( new Fragment1());
        fragments.add(new Fragment2());
        FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments);

        //设定适配器
        ViewPager vp = (ViewPager)findViewById(R.id.pager);
        vp.setAdapter(adapter);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        mScanGun.isMaybeScanning(keyCode, event);
          return super.onKeyDown(keyCode,event);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // 拦截物理键盘事件
        if (event.getKeyCode() > 6) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if(event.getKeyCode()==KeyEvent.KEYCODE_DEL){//将键盘的删除键传递下去
                    return super.dispatchKeyEvent(event);
                }
                this.onKeyDown(event.getKeyCode(), event);
            }
        }else{
            if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){//将键盘的返回事件传递下去
                return super.dispatchKeyEvent(event);
            }
        }
        return true;
    }


}
